package LogisticsSystem.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import LogisticsSystem.mapper.CustomerMapper;
import LogisticsSystem.mapper.StaffMapper;
import LogisticsSystem.po.Customer;
import LogisticsSystem.po.CustomerExample;
import LogisticsSystem.po.Staff;
import LogisticsSystem.po.StaffExample;
import LogisticsSystem.service.UserService;
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private CustomerMapper customerMapper;
	@Autowired
	private StaffMapper stafMapper;

	/**
	 * 传入账号密码，在数据库中查询该用户，有返回该用户对象，否则返回null
	 * 
	 * @param phone
	 * @param password
	 * @return
	 * @throws Exception
	 */
	public Customer customerlogin(String phone, String password) throws Exception {
		CustomerExample customerExample = new CustomerExample();
		LogisticsSystem.po.CustomerExample.Criteria createCriteria = customerExample.createCriteria();
		createCriteria.andCPhoneEqualTo(phone);
		List<Customer> customer = customerMapper.selectByExample(customerExample);
		if (customer != null && customer.size() > 0 && customer.get(0).getcPwd().equals(password)) {
			return customer.get(0);
		}
		return null;
	}

	/**
	 * 传入账号密码，在数据库中查询该用户，有返回该用户对象，否则返回null
	 * 
	 * @param phone
	 * @param password
	 * @return
	 * @throws Exception
	 */
	public Staff stafflogin(String phone, String password) throws Exception {

		StaffExample staffExample = new StaffExample();
		LogisticsSystem.po.StaffExample.Criteria createCriteria = staffExample.createCriteria();
		createCriteria.andSPhoneEqualTo(phone);
		createCriteria.andSPwdEqualTo(password);
		List<Staff> staff = stafMapper.selectByExample(staffExample);
		if (staff != null && staff.size() > 0) {
			return staff.get(0);
		}
		return null;
	}

	/**
	 * 传入一个Customer对象，向数据库插入该对象，成功返回true
	 */
	public Boolean insertCustomer(Customer customer) throws Exception {
		if (customer != null) {
			if (customerMapper.insert(customer)> 0) {// 插入语句有异议
				return true;
			} 
		}
		return false;
	}

	/**
	 * 传入手机号码，检查是否唯一
	 */
	public Boolean findCustomer(String phone) throws Exception{
		if(phone!=null) {
			CustomerExample customerExample = new CustomerExample();
			LogisticsSystem.po.CustomerExample.Criteria createCriteria = customerExample.createCriteria();
			createCriteria.andCPhoneEqualTo(phone);
			if(customerMapper.selectByExample(customerExample)==null) {
				return true;
			}
		}
		return false;
	}
	
	
}
